package com.itheima.health.mq;


import com.itheima.health.service.OrderService;
import com.itheima.health.utils.sms.SmsUtils;
import com.rabbitmq.client.Channel;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
public class SmsSendMessage {

    @Reference
    private OrderService orderService;

    @RabbitListener(queues = "sms-queue")  //  持续监听 一旦发现  立刻 调用阿里云 发送短信
    public void sendSms(Map map) {
        SmsUtils.validUserTelephone((String)map.get("telephone"),(String)map.get("code"));
    }

    @RabbitListener(queues = "sms-success-queue")  //  持续监听 一旦发现  立刻 调用阿里云 发送短信
    public void sendSuccessSms(Map map) {
        SmsUtils.orderSuccessMessage((String)map.get("telephone"),(String)map.get("code"));
    }

    @RabbitListener(queues = "dlx-queue")
    public void receive(String msg, Channel channel, Message message) throws IOException {
        try {
            System.out.println("msg = " + msg);
            orderService.updateOrderState(msg);
        } catch (Exception e) {
        }
    }
}
